﻿/*--------------------------------------------------
	GAS MODULE JAVASCRIPT FILE
	Author : Finance Google Gadget Team Oil/Gas Group

----------------------------------------------------*/
//Object that contains all useful information about current gas type

var GAS_TAB_ID = "gas_tab";

function GasInfo() {
	//Name
	this.name;
	// Price
	this.price;
}

//=============================================================
// parses html document whith gas information

function parseGasDataHtml (response){
	var gasValues = new Array();

	//regular expressions
	var arrOfResMos = /MOSCOW.*\n.*\$([1-9]*)\.([1-9]*)\</.exec(response);
	var arrOfResTok = /TOKYO.*\n.*\$([1-9]*)\.([1-9]*)\</.exec(response);
	var arrOfResSyd = /SYDNEY.*\n.*\$([1-9]*)\.([1-9]*)\</.exec(response);
	var arrOfResFran = /FRANKFURT.*\n.*\$([1-9]*)\.([1-9]*)\</.exec(response);
	var arrOfResRom = /ROME.*\n.*\$([1-9]*)\.([1-9]*)\</.exec(response);

	//filling gasVaues[]
	for (var i=0; i<5; i++){
	gasValues[i] = new GasInfo();
}

	gasValues[0].name = prefs.getMsg("moscow");
	gasValues[1].name = prefs.getMsg("tokyo");
	gasValues[2].name = prefs.getMsg("sydney");
	gasValues[3].name = prefs.getMsg("frankfurt");
	gasValues[4].name = prefs.getMsg("roma");

	//price with correct separator
	gasValues[0].price = arrOfResMos[1] + prefs.getMsg("decimal_symbol") + arrOfResMos[2];
	gasValues[1].price = arrOfResTok[1] + prefs.getMsg("decimal_symbol") + arrOfResTok[2];
	gasValues[2].price = arrOfResSyd[1] + prefs.getMsg("decimal_symbol") + arrOfResSyd[2];
	gasValues[3].price = arrOfResFran[1] + prefs.getMsg("decimal_symbol") + arrOfResFran[2];
	gasValues[4].price = arrOfResRom[1] + prefs.getMsg("decimal_symbol") + arrOfResRom[2];
	displayGasDataTable(gasValues);
}


//-------------------------------------------------------------------------------------
// makes table and inserts it in tab GAS_TAB_ID

function displayGasDataTable(gasValues) {

	var table =  "<table class = 'rate_table'>";
	table += "<thead><tr><td>" + prefs.getMsg("city") + "</td>";
	table += "<td>" + prefs.getMsg("date") + "</td>";
	table += "<td>" + prefs.getMsg("usd_for_gallon") + "</td></tr></thead><tbody>";

	for (var i = 0; i < 5; i++){
		if (i % 2 == 1){
			table += "<tr class = 'even'>";
		}else{
			table += "<tr class = 'odd'>";
		}
		table += "<td><b>" + gasValues[i].name + "</b></td>";
		table += "<td align=center>" + dateToString(new Date()) + "</td>";
		table +=  "<td align=right><b>" + gasValues[i].price + "</b></td></tr>";
	}
	_gel(GAS_TAB_ID).innerHTML = table + "</tbody></table>";
	_IG_AdjustIFrameHeight();
}

//====================================================================

function dateToString(date){
	var day = "";
	var month = "";
	if(date.getDate() < 10) day += "0";
	if(date.getMonth() < 10) month += "0";
	day+= date.getDate();
	month += date.getMonth() + 1;
	return day + "." + month;
}
//====================================================================
//Main function

function displayGasInfo(){
	showLoadingInfo(GAS_TAB_ID);
	var url = "http://money.cnn.com/pf/features/lists/global_gasprices/price.html ";
	_IG_FetchContent(url, _IG_Callback(parseGasDataHtml));
}